var Template = {

	selectedModule : "",
	selectedBlock : "",
	
    places : new Array(),
	
	/*
	 * Loading block
	 */
    loadBlock : function(module, block){
		Template.selectedModule = module;
		Template.selectedBlock = block;
		$("#appendToButtons").show(100);
		Cyfy.ajax("Cyfy", "getBlock", "#virtualPlace", "class=" + module + "&block=" + block, '$(".Cyfy__content").after($("#virtualPlace").html()); $("#virtualPlace").html(""); Template.init()');
    },
    init : function()
	{

		// BLOCK HOVER
		$(".block").hover(
		// mouse on
		function(){

			$(this).append("<div class=\"blockInfoPanel\">" + this.className + "<input type=\"submit\" class=\"blockRemoveButton\" value=\"X\" /></div>");
			$(".blockRemoveButton").click(function(){
				$(this).parent().parent().hide("fast", function(){ $(this).remove() });
			});
			$(this).addClass("blockActive");
		
			$(this).find("a").css({
				position: "relative",
				zIndex:101
			});
		}, 
		// mouse out
		function(){
		
			$(".blockInfoPanel").remove();
			$(this).removeClass("blockActive");
		
		});
		
    },
    

    save : function(){
		
		var send = "";
		
		for(var i = 0; i<Template.places.length; i++)
		{
			// place ... Template.places[i];
			var place =	Template.places[i];
			
			var a = 0;
			// for each block in place do
			$("#place-" + place + " .block").each(function()
			{
				var module = $(this).find(".templateModuleID").attr('value');
				var block = $(this).find(".templateBlockID").attr('value');
				send += "positions[" + place + "][" + a + "][module]=" + module + "&positions[" + place + "][" + a + "][block]=" + block +"&";
				a++;
			});
		
		}
		Cyfy.ajax("Templates", "setBlocks", "", send);
    }

}




/*
 * Autoexec
 */
$(document).ready(function(){
     
	 		// BLOCKS SORTABLE
		$(".place").sortable({
			connectWith: '.place',
			helper: 'clone',
			forcePlaceholderSize: true,
			placeholder: 'blockPlaceholder' ,
			start: function(event, ui){
				$(".bodyOver").fadeTo("slow", 0.4);
			},
			stop: function(event, ui){
				$(".bodyOver").fadeTo("slow", 0);
			}
		}).disableSelection();
		//$(".place").dropable();
		
		$(".blockLI").dblclick(function(){
		
			$(this).remove();
		});
				// over
		$("body").prepend("<div class=\"bodyOver\" />");
		$(".bodyOver").css({
			position: 'absolute',
			top: '0px',
			backgroundColor: '#000000',
			left: '0px',
			zIndex: '10',
			width: $("body").outerWidth( true ),
			height: $("body").outerHeight( true )
		});
		
		$(".bodyOver").fadeTo(0,0);
		
	/*	
	// PLACES
	$(".place").hover(
		// mouse on
		function(){
			$(this).css({
				minHeight: "40px",
			});
		}, 
		// mouse out
		function(){
			$(this).css({
				minHeight: "2px",
			});
		
		});
	*/
    
	$("#appendToButtons input").click(function(){
	
		if(Template.selectedBlock && Template.selectedModule){
			
			$("#place-" + $(this).val()).append($("#virtualPlace").html());
			$("#virtualPlace").html("");
			$(".blocks-blocks").selectedIndex = 0;
			$(".blocks-modules").selectedIndex = 0;
			$("#appendToButtons").hide(100);
			
			Template.init();
		}
	
	});
	// BLOCKS
    Template.init();

	$("#appendToButtons").hide();


    
});

